package com.sos.widget.view.floatWIndow;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.sos.widget.app.DisplayUtil;
import com.sos.widget.log.LogUtils;

import java.util.List;

import static android.content.Context.ACTIVITY_SERVICE;


public class FloatWindowManager {
    private WindowManager myWindowManger;
    private FloatViewParams floatViewParams;

    public void initWWindowManager(Context context, View view) {
        floatViewParams = initFloatViewParams(context, view);
        myWindowManger = SystemUtils.getWindowManager(context);
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        //设置包名
        layoutParams.packageName = context.getPackageName();
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_SCALED
                | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        //系统弹窗
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//6.0
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type =  WindowManager.LayoutParams.TYPE_PHONE;
        }


//        layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;;
        //弹窗位置
        layoutParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        //位图格式
        layoutParams.format = PixelFormat.RGBA_8888;
        layoutParams.width = floatViewParams.width;
        layoutParams.height = floatViewParams.height;
        myWindowManger.addView(view, layoutParams);

    }

    public boolean isRunningForeground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
        // 枚举进程
        for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfos) {
            if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                if (appProcessInfo.processName.equals(context.getApplicationInfo().processName)) {
                    return true;
                }
            }
        }
        return false;
    }



    /**
     * 初始化窗口参数
     *
     * @param mContext
     * @return
     */
    private FloatViewParams initFloatViewParams(Context mContext, View view) {
        FloatViewParams params = new FloatViewParams();
        //根据实际宽高和设计稿尺寸比例适应。
        //设置窗口大小，已view、视频大小做调整
        int[] size = new int[2];
        int width_size = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int height_size = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(width_size, height_size);
        size[0] = view.getMeasuredWidth();
        size[1] = view.getMeasuredHeight();
        int winWidth = size[0];
        int winHeight = size[1];
        int width = winWidth;

        float ratio = 1.0f * winHeight / winWidth;
        int height = (int) (width * ratio);

        //如果上次的位置不为null，则用上次的位置
        int windowsWidth = DisplayUtil.getWindowsWidth(mContext);
        params.width = windowsWidth;
        params.height = height;
        LogUtils.i("windowsWidth"+windowsWidth+"--width"+width);
        return params;
    }

    public void openFloatWindow() {
    }

    public void closeFloatWindow(View view) {
        if (myWindowManger != null && view != null) {
            try {
                myWindowManger.removeView(view);
            } catch (Exception e) {

            }

        }
    }

    public void updateFloatWindow() {
    }
}
